package com.lonewalker.completablefuturedemo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author LoneWalker
 * @date 2023/2/12
 * @description
 */
public class demo2 {
    public static void main(String[] args) {
        CompletableFuture<Integer> completedFuture1 = CompletableFuture.supplyAsync(() ->
        {
            return 7;
        });
        // join抛出的unchecked异常不需要代码中处理
        int joinResult = completedFuture1.join();
        System.out.println(joinResult);

        // get抛出的是受查异常，需要向上throws或者try-catch包围
        int getResult = 0;
        try {
            getResult = completedFuture1.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(getResult);


        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 8;
        });
        int nowResult = completableFuture2.getNow(9);
        System.out.println(nowResult);
    }
}
